﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Claro.QuestionModel
{

    //public class TiposEventos : SpecializedEnum<TiposEventos, string>
    //{
    //    public static readonly string Avaliacao = "A";
    //    public static readonly string Competicao = "C";
    //}


    public class clsEvento
    {
        public int codEvento { get; set; }
        public string Descricao { get; set; }
        public DateTime Data { get; set; }
        public clsCurso Curso { get; set; }
        public List<clsGrupo> Grupos { get; set; }
        public List<clsAssunto> Assuntos { get; set; }
        public string TipoEvento;

        //Faz desse objeto um Singleton
        private static clsEvento referencia;

        public static clsEvento getInstance()
        {
            if (referencia == null)
                referencia = new clsEvento();

            return referencia;
        }

        public clsEvento()
        {
            this.Grupos = new List<clsGrupo>();
            this.Assuntos = new List<clsAssunto>();
            this.Curso = new clsCurso();
        }

        public void Salvar()
        {
            SqlConnection cn = clsConexao.Conectar();
            SqlCommand cmd= cn.CreateCommand();
            cmd.CommandText = "INSERT Evento (Descricao, Data, codTipoEvento, codStatus) values (@Descricao, @Data, @codTipoEvento, @codStatus)";
            cmd.Parameters.Add("@Descricao",SqlDbType.VarChar, 50).Value = this.Descricao;
            cmd.Parameters.Add("@Data", SqlDbType.DateTime).Value = this.Data;
            cmd.Parameters.Add("@codTipoEvento", SqlDbType.Char, 1).Value = this.TipoEvento ;
            cmd.Parameters.Add("@codStatus", SqlDbType.Char, 1).Value = Status.Aberto;
            cmd.ExecuteNonQuery();

            cmd = cn.CreateCommand();
            cmd.CommandText = "SELECT @@Identity";
            this.codEvento = Int32.Parse(cmd.ExecuteScalar().ToString());

            foreach (clsAssunto assunto in this.Assuntos)
            {
                cmd = cn.CreateCommand();
                cmd.CommandText = "INSERT EventoAssunto (codEvento, codAssunto) values (@codEvento, @codAssunto)";
                cmd.Parameters.Add("@codEvento", SqlDbType.Int).Value = this.codEvento;
                cmd.Parameters.Add("@codAssunto", SqlDbType.Int).Value = assunto.codAssunto;
                cmd.ExecuteNonQuery();
            }

            cn.Close();
            cn.Dispose();
        }
        
        public void CriarGrupos()
        {
            foreach (clsGrupo grupo in this.Grupos)
            {
                grupo.Criar(this.codEvento);
            }
        }

        public void AlterarCurso()
        {
            SqlConnection cn = clsConexao.Conectar();
            SqlCommand cmd = cn.CreateCommand();
            cmd.CommandText = "Update evento set codCurso = @codCurso where codEvento = @codEvento";
            cmd.Parameters.Add("@codCurso", SqlDbType.Int).Value = this.Curso.codCurso;
            cmd.Parameters.Add("@codEvento", SqlDbType.Int).Value = this.codEvento;
            cmd.ExecuteNonQuery();

            cn.Close();
            cn.Dispose();
        }

        public DataTable ExibirPlacar()
        {
            string sql = "ExibirPlacar";

            SqlConnection cn = clsConexao.Conectar();
            SqlCommand cmd = cn.CreateCommand();
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@codEvento", SqlDbType.Int).Value = this.codEvento;
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataTable dt = new DataTable();
            da.Fill(dt);
            
            cn.Close();
            cn.Dispose();

            return dt;

        }

        public bool LocalizarEventoAberto()
        {
            string sql = "select codEvento, Descricao, Data from evento where codStatus = 'A'";
            
            SqlConnection cn = clsConexao.Conectar();
            SqlCommand cmd = cn.CreateCommand();
            cmd.CommandText = sql;
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                this.codEvento = dr.GetInt32(dr.GetOrdinal("codEvento"));
                this.Descricao = dr.GetString(dr.GetOrdinal("Descricao"));
                this.Data = dr.GetDateTime(dr.GetOrdinal("Data"));
                cn.Close();
                cn.Dispose();

                return true;
            }
            cn.Close();
            cn.Dispose();
            return false;
        }

        public bool PreencherGruposEventoAberto()
        {
            this.Grupos = clsGrupo.ListarGruposEvento(this);
            return (this.Grupos.Count > 0);
        }


    }
}
